package com.diandi.messages;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.diandi.messages.Application;
import com.diandi.messages.bean.phone.MessageMsg;
import com.diandi.messages.controller.phone.MessageMsgController;
import com.diandi.messages.enums.MessagesMsgEnums;
import com.diandi.messages.manager.phone.MessageMsgManager;
import com.diandi.messages.service.phone.MessageMsgService;
import com.diandi.messages.tools.CreateTradeNo;
import com.diandi.messages.tools.GsonUtil;
import com.diandi.messages.util.AliBigFishUtil;
import com.diandi.messages.util.AliSMSDto;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.google.gson.Gson;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpSession;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.RequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import static org.hamcrest.Matchers.*;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;


/**
 * 单元测试
* @author mwz
* @create 2017-09-26 14:17
* @version V1.0.0
*/


/*RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes=Application.class)// 指定spring-boot的启动类*/

@RunWith(SpringJUnit4ClassRunner.class)

@SpringApplicationConfiguration(Application.class)
@AutoConfigureMockMvc
@WebAppConfiguration
public class PhoneServiceTest {


    @Autowired
    MessageMsgService messageMsgService;

    @Autowired
    MessageMsgManager messageMsgManager;

    @Autowired
    MessageMsgController messageMsgController;

    @Autowired
    private WebApplicationContext wac; // 注入WebApplicationContext

    /*    @Autowired
    private MockHttpSession session;// 注入模拟的http session
//
    @Autowired
    private MockHttpServletRequest request;// 注入模拟的http request\*/

    private MockMvc mockMvc; // 模拟MVC对象，通过MockMvcBuilders.webAppContextSetup(this.wac).build()初始化。

    @Before // 在测试开始前初始化工作
    public void setup() {
        this.mockMvc = MockMvcBuilders.webAppContextSetup( this.wac ).build();
    }





    public  void getMessages(){
        Map<String,String> queryMap = new HashMap<String,String>();
        queryMap.put("message_trade","TR2017092514220001006356943325");
        MessageMsg messageMsg =  messageMsgService.getMessageMsg( queryMap );
        System.out.println( "message_trade:"+ messageMsg.getMessageTrade());
    }



    public  void sendMessages(){
        Map<String,String> queryMap = new HashMap<String,String>();
        queryMap.put("message_trade","TR2017092514220001006356943325");
       /* //MessageMsg messageMsg =  messageMsgService.sendMessages( queryMap );
        System.out.println( "message_trade:"+ messageMsg.getMessageTrade());*/
    }


    public void saveMesgae(){
        Map<String,String> queryMap = new HashMap<String,String>();
        queryMap.put("messageTrade","TR2017092514220001006356943323");
        queryMap.put("messageBatchTrade","TR2017092514220001006356943325");
        queryMap.put("businessId","TR2017092514220001006356943325");
        queryMap.put("passCode","100001");
        queryMap.put("merchantNo","100015");
        queryMap.put("userId","121312");
        queryMap.put("msgTemplateCode","ms10011");
        queryMap.put("phoneId","18501373659");
        queryMap.put("requestMsg","sdfksjkdfjksjdfkajskdfjksdjfkjsakdjfk");
       int rtnCont = messageMsgManager.saveMessageData(queryMap);
        System.out.println( "insert:"+rtnCont );
    }

    public void updateMesgae(){
        Map<String,String> queryMap = new HashMap<String,String>();
        queryMap.put("messageTrade","TR2017092514220001006356943323");

        queryMap.put("businessId","TR2017092514220001006356943323");
        queryMap.put("passCode","100001");
        queryMap.put("merchantNo","100015");
        queryMap.put("userId","121312");


        queryMap.put("requestMsg","sdfksjkdfjksjdfkajskdfjksdjfkjsakdjfk");
        int rtnCont = messageMsgManager.updateMessageData(queryMap, MessagesMsgEnums.处理成功.getNumber());
        System.out.println( "insert:"+rtnCont );
    }



    public void sendAlMessage(){
        Map<String,Object> msgMap = new HashMap<String, Object>();
        Map<String,String> rtnMap = new HashMap<String, String>();
        AliSMSDto aliSMSDto = new AliSMSDto();
        aliSMSDto.setMobilePhone("15810633129,18947170519,13722116907,18501373659");
        //msgMap.put("customer","点滴客户");
        aliSMSDto.setSmsFreeSignName("大鱼测试");
        aliSMSDto.setSmsParam(msgMap);
        aliSMSDto.setUserId("123123");
        aliSMSDto.setTemplateCode("SMS_11560845");
        rtnMap = AliBigFishUtil.sendMsg(aliSMSDto);
        System.out.println(rtnMap.toString());

    }


    public void sendMessage() throws Exception {
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("customer1","点滴客户");
        paramMap.put("customer2","点滴客户");

        System.out.println( JSONObject.toJSONString(paramMap));
        Map<String, Object> mapSub1 = new HashMap<>();
        mapSub1.put("userId","12321312312");
        mapSub1.put("businessId","12321312312");
        mapSub1.put("msgTemplateCode","SMS_11560845");
        mapSub1.put("phone","15810633129");
        mapSub1.put("paramList", JSONObject.toJSONString(paramMap));
        System.out.println( mapSub1 );
        System.out.println( mapSub1.toString() );
        Map<String, Object> mapSub2 = new HashMap<>();
        mapSub2.put("userId","12321312312");
        mapSub2.put("businessId","12321312312");
        mapSub2.put("msgTemplateCode","SMS_11560845");
        mapSub2.put("phone","15810633129");
        mapSub2.put("paramList", JSONObject.toJSONString(paramMap));




        List<Map<String,Object> > listMap = new ArrayList<Map<String,Object>>();
        listMap.add(mapSub1);
        listMap.add(mapSub2);
        System.out.println(  "map:" + listMap );
        Map<String, Object> map = new HashMap<>();
        map.put("passCode", "1000101");
        map.put("merchantNo", "100010115");
        map.put("msgList",JSONObject.toJSONString(listMap));
        System.out.println(  "mapList:" + map );

        System.out.println(  "mapJson:" + JSONObject.toJSONString(map) );


       /* Map<String, Object> rtnMap  = gson.fromJson(tt,Map.class);
        System.out.println( "mwzMap:"+ gson.toJson(rtnMap));
        System.out.println( "mwzMap:"+ gson.toJson(rtnMap.get("msgList")));
        List<Map<String, String>> msgList = new ArrayList<Map<String, String>>();
        JSONArray msgListArray = JSONArray.parseArray( gson.toJson(rtnMap.get("msgList")) );
        Map<String, String> msgListMap = null;
        for (Object msgInfo : msgListArray) {
            msgListMap = gson.fromJson(tt,Map.class);
            msgList.add(msgListMap);
        }*/
       // System.out.println( "--msgList--"+msgList.size());
        //调用接口，传入添加的用户参数
      /*  ObjectMapper mapper = new ObjectMapper();
        mockMvc.perform(post("/sendMessageMsg")
                .contentType(MediaType.APPLICATION_JSON_UTF8)
                .content(mapper.writeValueAsString(map)));*/
/*

        JSONObject param = new JSONObject() ;
        param.put("passCode", "1000101");
        param.put("merchantNo", "100010115");
        String jsonstr = map.toString() ;
        System.out.println("================================请求入参："+jsonstr);
        RequestBuilder request = MockMvcRequestBuilders.post("uri")
                .contentType(MediaType.APPLICATION_JSON_UTF8)
                .header("SESSIONNO", "")
                .content(jsonstr) ;

        MvcResult mvcResult = mockMvc.perform(request).andReturn() ;

        int status = mvcResult.getResponse().getStatus();
        String content = mvcResult.getResponse().getContentAsString();

        Assert.assertTrue("正确", status == 200);
        Assert.assertFalse("错误", status != 200);

        System.out.println("返回结果："+status);
        System.out.println(content);
*/



       /* MvcResult result = mockMvc.perform( post("/sendMessageMsg").contentType(MediaType.APPLICATION_FORM_URLENCODED).content(JSONObject.toJSONString(map)))
                .andExpect(status().isOk())// 模拟向testRest发送get请求
                .andExpect(content().contentType(MediaType.APPLICATION_FORM_URLENCODED))// 预期返回值的媒体类型text/plain;charset=UTF-8
                .andReturn();// 返回执行请求的结果*/
    /*    System.out.println(result.getResponse().getContentAsString());
        String platTradeNo = CreateTradeNo.createTradeNo("TR");
        HttpServletRequest request = null;*/
       // messageMsgService.sendMessages(request,platTradeNo);
    }


    public void TestJosn(){
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("customer1","点滴客户");
        paramMap.put("customer2","点滴客户");

        System.out.println( JSONObject.toJSONString(paramMap));
        Map<String, Object> mapSub1 = new HashMap<>();
        mapSub1.put("userId","12321312312");
        mapSub1.put("businessId","12321312312");
        mapSub1.put("msgTemplateCode","SMS_11560845");
        mapSub1.put("phone","15810633129");
        mapSub1.put("paramList", JSONObject.toJSONString(paramMap));
        System.out.println( mapSub1 );
        System.out.println( mapSub1.toString() );
        Map<String, Object> mapSub2 = new HashMap<>();
        mapSub2.put("userId","12321312312");
        mapSub2.put("businessId","12321312312");
        mapSub2.put("msgTemplateCode","SMS_11560845");
        mapSub2.put("phone","15810633129");
        mapSub2.put("paramList", JSONObject.toJSONString(paramMap));




        List<Map<String,Object> > listMap = new ArrayList<Map<String,Object>>();
        listMap.add(mapSub1);
        listMap.add(mapSub2);
        System.out.println(  "map:" + listMap );
        Map<String, Object> map = new HashMap<>();
        map.put("passCode", "1000101");
        map.put("merchantNo", "100010115");
        map.put("msgList",JSONObject.toJSONString(listMap));
        System.out.println(  "mapList:" + map );

        System.out.println(  "mapJson:" + JSONObject.toJSONString(map) );
    }

    @Test
    public void JsonMap() {
        Map<String, String> paramMap1 = new HashMap<String, String>();
        paramMap1.put("customer1","点滴客户");
        paramMap1.put("customer2","点滴客户");

        Map<String, String> paramMap2 = new HashMap<String, String>();
        paramMap2.put("customer1","点滴客户");
        paramMap2.put("customer2","点滴客户");

        Map<String, Object> mapSub1 = new HashMap<>();
        mapSub1.put("userId","12321312312");
        mapSub1.put("businessId","12321312312");
        mapSub1.put("msgTemplateCode","SMS_11560845");
        mapSub1.put("phone","15810633129");
        mapSub1.put("paramList", paramMap2);

        Map<String, Object> mapSub2 = new HashMap<>();
        mapSub2.put("userId","12321312312");
        mapSub2.put("businessId","12321312312");
        mapSub2.put("msgTemplateCode","SMS_11560845");
        mapSub2.put("phone","15810633129");
        mapSub2.put("paramList", paramMap1 );




        List<Map<String,Object> > listMap = new ArrayList<Map<String,Object>>();
        listMap.add(mapSub1);
        listMap.add(mapSub2);

        Map<String, Object> map = new HashMap<>();

        map.put("msgList",listMap );
        System.out.println(  "mapList:" + map );

        System.out.println(  "mapJson:" + JSONObject.toJSONString(map) );
    }
}
